package com.ziytek.web.citizen.model.bean;

import com.ziytek.web.citizen.constant.ServerConstant;
import lombok.Data;

/**
 * @author fenghx
 * @version v1.0
 * @className CapacityProtocol
 * @descripton 电池容量数据指令下发
 * @date 2023/4/27 15:49
 **/
@Data
public class CapacityProtocol {
    private String header = "BB";

    /**
     * 通信板地址
     */
    private String boardAddress = "00";
    /**
     * 数据类型
     * 0X01 为周期数据，周期数据定时上送包含设备端所有的电芯数据
     * 0x02 为设置命令，pc端对设备设置充放电的上下限
     * 0x03 控制板请求上位机的时间戳
     * 0x04 上位机向控制板发送配置协议
     */
    private String dataType = "08";
    /**
     * 有效数据长度
     */
    private String dataLen = "000D";

    /**
     * 有效数据
     */
    private String data;

    private String xor;
    /**
     * 数据尾 固定 CC
     */
    private String dataTail = "CC";


    private String getSeparatorData() {
        String d = this.data;

        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < d.length() / 2; i++) {
            if (i == ((d.length() / 2) - 1)) {
                buffer.append(d, i * 2, (i + 1) * 2);
            } else {
                buffer.append(d, i * 2, (i + 1) * 2).append(ServerConstant.DATA_SEPARATOR_BLANK_SPACE);
            }
        }

        return buffer.toString();
    }

    public String getAllData() {
        return this.header + ServerConstant.DATA_SEPARATOR_BLANK_SPACE
                + this.boardAddress + ServerConstant.DATA_SEPARATOR_BLANK_SPACE
                + this.dataType + ServerConstant.DATA_SEPARATOR_BLANK_SPACE
                + this.dataLen + ServerConstant.DATA_SEPARATOR_BLANK_SPACE
                + this.getSeparatorData() + ServerConstant.DATA_SEPARATOR_BLANK_SPACE
                + this.xor + ServerConstant.DATA_SEPARATOR_BLANK_SPACE
                + this.dataTail;
    }
}
